/**
 * 期费用预提
 * @author shixc
 * @date 2019/10/25
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_where = {},
        cols = [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'GWFCDOCNO', width: 160, title: '费用预提单号', fixed: 'left', sort: true}
            , {field: 'GWFCSEQ', width: 60, title: '项次'}
            , {field: 'GWFC001', width: 160, title: '年度', sort: true, search: 'default', searchOpts: {type: 'year'}}
            , {
                field: 'GWFC002', width: 160, title: '月份', sort: true, searchOpts: {type: 'select', default: ''},
                filterDatas: app.datas_('crm_gwat550_qb')
            }
            , {field: 'GWFC003_NAME', width: 80, title: '区域', sort: true}
            , {field: 'GWFC004_NAME', width: 120, title: '客户', sort: true}
            , {field: 'GWFC005_NAME', width: 150, title: '品类', sort: true}
            , {field: 'GWFC006_NAME', width: 60, title: '业务组织', sort: true}
            , {field: 'GWFC007_NAME', width: 100, title: '费用'}
            , {field: 'GWFC100_NAME', width: 100, title: '币别'}
            , {field: 'GWFC101', width: 100, title: '汇率'}
            , {field: 'GWFC102', width: 80, title: '预测销售原币金额'}
            , {field: 'GWFC103', width: 80, title: '预测销售本币金额'}
            , {field: 'GWFC104', width: 100, title: '区域费率'}
            , {field: 'GWFC105', width: 100, title: '总部费率'}
            , {field: 'GWFC106', width: 100, title: '区域预提费用原币金额', sort: true}
            , {field: 'GWFC107', width: 100, title: '区域预提费用本币金额'}
            , {field: 'GWFC108', width: 100, title: '总部预提费用原币金额'}
            , {field: 'GWFC109', width: 160, title: '总部预提费用本币金额'}
            , {field: 'GWFCACTI', width: 60, title: '审核否', filterDatas: app.datas_('crm_gwei005_gwecconf')},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwft200setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwft200SettingAction
    ;

    var Gwft200 = app.Module.extend({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwft200',
                _searchs: [{
                    id: 'gwft200', tableId: 'gwft200_table', datas: _searchsArr.pushArray([
                        {
                            field: 'GWFC003',
                            title: '区域',
                            search: 'default',
                            searchOpts: {icon: true}
                        }
                        , {
                            field: 'GWFC004',
                            title: '客户',
                            search: 'default',
                            searchOpts: {icon: true}
                        }, {
                            field: 'GWFC005',
                            title: '品类',
                            search: 'default',
                            searchOpts: {icon: true}
                        }
                        , {
                            field: 'GWFC006',
                            title: '业务组织',
                            search: 'default',
                            searchOpts: {icon: true}
                        }
                    ])
                }],
                _loadTplsFull: [
                    {
                        dom: 'gwft200_edit', tpl: 'gwft200_edit'
                    },
                    {dom: 'common_open_window', tpl: 'common/_open_window'},
                    {dom: 'gwft200_auto', tpl: 'gwft200_auto'}
                ]
            }, win);
        },

        initHeadTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwft200_table'),
                    url: g_ctxPath + 'crm/gwft200/getList',
                    // exportsUrl: g_ctxPath + 'crm/gwai300/getHeadlist',
                    // exportsName: '客户数据',
                    title: '期费用预提数据',
                    method: 'post',
                    id: 'gwft200_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwft200setting-action').innerHTML).render({}, function (string) {
                    gwft200SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwft200_table)', function (obj) {

                    layer.tips(gwft200SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwft200_edit',
                                            tagData:{
                                                obj:obj,
                                                isAdd:false,
                                                isCheck:false
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: '期费用预提编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwft200_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwft200_edit',
                        tagData: {
                            obj:obj,
                            isAdd: false,
                            isCheck:true
                        },
                        index: 0,
                        openOpts: {
                            title: '期费用预提编辑',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwft200_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwft200_edit',
                                tagData: {
                                    is_add: true,
                                    isCheck: false
                                },
                                index: 0,
                                openOpts: {
                                    title: '期费用预提编辑',
                                    maxmin: true
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.delDataBatch();
                                }
                            });
                            break;
                        case 'batch':
                            self._openTplFull({
                                tag: 'gwft200_auto',
                                tagData: {},
                                index: 2,
                                openOpts: {
                                    title: '批次产生',
                                    area: ['500px', '430px']
                                }
                            });
                            break;
                        case 'check' :
                            self.check();
                            break;
                        case 'unCheck' :
                            self.unCheck();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwft200_table', where: table_where});
        },

        delData: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwft200/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '期费用预提删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwft200_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                var _choose = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWFC_T');
                    if (v.GWFCACTI != 'N') {
                        app.showWarning("只有状态为N：未审核的数据才可删除！");
                        flag = false;
                        return;
                    } else {
                        if(_choose.indexOf(v.GWFCDOCNO) == -1){
                            _choose.push(v.GWFDDOCNO);
                            _temp.GWFCENT = v.GWFCENT;
                            _temp.GWFCDOCNO = v.GWFCDOCNO;
                            _xdata.push(_temp);
                        }

                    }
                });
            } else {
                app.showWarning("请先选择单据！");
                return;
            }
            if (flag) {
                app.ajax({
                    xurl: 'crm/gwft200/deleteDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '期费用预提删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        check: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwft200_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                var data = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWFC_T');
                    if (v.GWFCACTI != 'N') {
                        app.showWarning("只有状态为N：未审核的数据才可以进行审核操作！");
                        flag = false;
                        return;
                    } else {
                        if(data.indexOf(v.GWFCDOCNO) == -1) {
                            data.push(v.GWFCDOCNO);
                            _temp.GWFCENT = v.GWFCENT;
                            _temp.GWFCDOCNO = v.GWFCDOCNO;
                            _temp.GWFCACTI = "Y";
                            _xdata.push(_temp);
                        }
                    }
                });
            } else {
                app.showWarning("请先选择数据！");
                return;
            }
            if (flag) {
                app.confirm({
                    msg: '是否审核选择的数据？',
                    ok: function () {
                        app.ajax({
                            xurl: 'crm/gwft200/updateStatus',
                            xdata: _xdata,
                            loading: true,
                            toJsonStr: true,
                            okmsg: '期费用预提审核成功',
                            cb: function (result) {
                                if (result.ok) {
                                    self.reloadTableCurr();
                                }
                            }
                        });
                    }
                });
            }
        },

        unCheck: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwft200_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                var data = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWFC_T');
                    if (v.GWFCACTI != 'Y') {
                        app.showWarning("只有状态为Y：已审核的数据才可以进行取消审核操作！");
                        flag = false;
                        return;
                    } else {
                        if(data.indexOf(v.GWFCDOCNO) == -1) {
                            data.push(v.GWFCDOCNO);
                            _temp.GWFCENT = v.GWFCENT;
                            _temp.GWFCDOCNO = v.GWFCDOCNO;
                            _temp.GWFCACTI = "N";
                            _xdata.push(_temp);
                        }
                    }
                });
            } else {
                app.showWarning("请先选择数据！");
                return;
            }
            if (flag) {
                app.confirm({
                    msg: '是否取消审核选择的数据？',
                    ok: function () {
                        app.ajax({
                            xurl: 'crm/gwft200/updateStatus',
                            xdata: _xdata,
                            loading: true,
                            toJsonStr: true,
                            okmsg: '期费用预提取消审核成功',
                            cb: function (result) {
                                if (result.ok) {
                                    self.reloadTableCurr();
                                }
                            }
                        });
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwft200',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWFC003') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwad002',
                                            columnList: [
                                                {label: '区域编号', prop: 'GWAD002', width: '150'},
                                                {label: '区域名称', prop: 'GWADL004', width: '150'},
                                                {label: '助记码', prop: 'GWADL005', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWFC003 = data.GWAD002;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择区域',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field == 'GWFC004') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWFC004 = data.GWAK001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户编号',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field == 'GWFC005') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwao001',
                                            columnList: [
                                                {label: '产品系列编号', prop: 'GWAO001', width: '150'},
                                                {label: '说明', prop: 'GWAOL003', width: '150'},
                                                {label: '助记码', prop: 'GWAOL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWFC005 = data.GWAO001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择品类',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field == 'GWFC006') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysorg_id',
                                            columnList: [
                                                {label: '部门编号', prop: 'ID', width: '150'},
                                                {label: '部门名称', prop: 'NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWFC006 = data.ID;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择业务组织',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initHeadTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwft200();

});